﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DTO;

namespace DAO
{
    public class DAO_Sach
    {
        public static object SelectAll_Sach()
        {
            try
            {
                NHASACHDataContext dcNhaSach = new NHASACHDataContext();
                var vSach = from sach in dcNhaSach.Saches
                            join nXB in dcNhaSach.NXBs on sach.MaNXB equals nXB.MaNXB
                            orderby sach.MaSach
                            select new
                            {
                                sach.MaSach,
                                sach.TenSach,
                                sach.MaTL,
                                sach.MaNXB,
                                nXB.TenNXB,
                                sach.Tacgia,
                                sach.NoiDung,
                                sach.Hinh,
                                sach.SLTon,
                                sach.DonGia,
                            };
                return vSach;
            }
            catch (System.Exception ex)
            {
                string str = ex.Message;
                return null;
            }
        }

        public static bool Insert_Sach(Sach sDTO)
        {
            try
            {
                NHASACHDataContext dcNhaSach = new NHASACHDataContext();
                dcNhaSach.Saches.InsertOnSubmit(sDTO);
                dcNhaSach.SubmitChanges();

                return true;
            }
            catch (System.Exception)
            {
                return false;
            }
        }

        public static bool Delete_Sach(Sach sDTO)
        {
            try
            {
                NHASACHDataContext dcNhaSach = new NHASACHDataContext();
                dcNhaSach.Saches.DeleteOnSubmit(sDTO);
                dcNhaSach.SubmitChanges();
                return true;
            }
            catch (System.Exception)
            {
                return false;
            }
        }

        public static bool Update_Sach(Sach sDTO)
        {
            try
            {
                NHASACHDataContext dcNhaSach = new NHASACHDataContext();
                Sach sach = dcNhaSach.Saches.Single(t => t.MaSach == sDTO.MaSach);
               
                sach.TenSach = sDTO.TenSach;
                sach.MaTL = sDTO.MaTL;
                sach.MaNXB = sDTO.MaNXB;
                sach.Tacgia = sDTO.Tacgia;
                sach.NoiDung = sDTO.NoiDung;
                sach.Hinh = sDTO.Hinh;
                sach.SLTon = sDTO.SLTon;
                sach.DonGia = sDTO.DonGia;

                dcNhaSach.SubmitChanges();
                return true;
            }
            catch (System.Exception)
            {
                return false;
            }
        }
    }
}
